import { _decorator, Component, Node, Sprite, SpriteAtlas, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import { PokerPoints } from "../constant/PokerPoints";
import { Poker } from "../data/Poker";

export type CardTouchCallback = (poker: Poker, isSelected: boolean) => void

/**
 * 卡牌控制类
 * 会被挂载到card预制体上
 * 实现触摸上升，和触摸事件通知
 */

@ccclass('CardCtrl')
export default class CardCtrl extends Component {
    

    @property(SpriteAtlas)
    cardSpriteAtlas: SpriteAtlas = null;

    //牌背面-除前缀文件名称
    private cardBackFileName = "02"
    // private cardBackFileName = 55
    public poker: Poker = null
    private isSelected = false
    private touchListeners: Set<Function | CardTouchCallback> = new Set()

    start() {

    }

    public set touchEnable(value: boolean) {
        if (value) {
            this.initTouchEvent()
        } else {
            this.offTouchEvent()
        }
    }

    public init(poker?: Poker): void {
        // console.log("cardCtrl.init()===============")
        // console.log(poker)

        this.poker = poker
        // this.initTouchEvent()
        if (this.poker) {
            this.show()
        }
    }

    /**
     * 显示牌面
     * //(花色*100)+牌点数，方便带花色比较大小[Value/100=整除是花色,Value%100=取模是牌点数
     * 服务器直接返回 val = (suit*100)+牌点数
     */
    private show(): void {
        // console.log("cardCtrl.show()===============")
        const cardSprite = this.node.getComponent(Sprite);
        let spriteFrameKey = Number(this.cardBackFileName); // 默认显示背面
        
        if (this.poker.point >= 1 && this.poker.point <= 13) {
            // 常规 1-13，即A~K
            // spriteFrameKey = (this.poker.suit * 13) + this.poker.point
            spriteFrameKey = (this.poker.suit * 100) + this.poker.point
        } else if (this.poker.point == PokerPoints.P_BJ) {
            // 小王
            // spriteFrameKey = 54
            spriteFrameKey = (5 * 100) + this.poker.point
        } else if (this.poker.point == PokerPoints.P_RJ) {
            // 大王
            // spriteFrameKey = 53
            spriteFrameKey = (5 * 100) + this.poker.point
        }
        // console.log("spriteFrameKey==============="+spriteFrameKey)
        cardSprite.spriteFrame = this.cardSpriteAtlas.getSpriteFrame('card_'+spriteFrameKey)
        
        // console.log('card_'+spriteFrameKey)
        // console.log(this.cardSpriteAtlas)
        // console.log(this.cardSpriteAtlas.getSpriteFrame('card_'+spriteFrameKey))
    }

    /**
     * 显示背面
     */
    public showBack(): void {
        const cardSprite = this.node.getComponent(Sprite)
        cardSprite.spriteFrame = this.cardSpriteAtlas.getSpriteFrame('card_'.concat(this.cardBackFileName));//拼接字符串成为牌的文件名
    }

    /**
     * 初始化卡牌触摸事件
     */
    private initTouchEvent() {
        console.log("CardCtrl.initTouchEvent")
        this.node.on(Node.EventType.TOUCH_START, this.onTouchEvent.bind(this))
    }

    private offTouchEvent() {
        this.node.off(Node.EventType.TOUCH_START)
    }

    /**
     * 触摸事件
     * 设置卡片上升或下降
     */
    private onTouchEvent() {
        console.log("CardCtrl.onTouchEvent-----")
        const distance = 20
        // 上升或下降
        if (this.isSelected) {
            // 下降
            tween(this.node)
                // .to(0.1, { position: new Vec3(0, 0, 0) })
                .by(0.1, { position: new Vec3(0, -distance, 0) })
                .start()
        } else {
            // 上升
            tween(this.node)
                // .to(0.1, { position: new Vec3(0, distance, 0) })
                .by(0.1, { position: new Vec3(0, distance, 0) })
                .start()
        }
        this.isSelected = !this.isSelected
        console.log("this.isSelected-----", this.isSelected)
        // 触发所有监听器
        this.touchListeners.forEach(callback => {
            console.log("touchListeners.forEach == ", this.isSelected)
            callback(this.poker, this.isSelected)
        })
    }

    public addTouchListener(callback: Function | CardTouchCallback): Function {
        console.log("CardCtrl.addTouchListener--------------------------")
        this.touchListeners.add(callback)
        return callback
    }

    public clearTouchListener(callback?: Function | CardTouchCallback) {
        if (callback == null) {
            // 清空所有监听器
            this.touchListeners.clear()
            return
        }
        this.touchListeners.delete(callback)
    }

}


